﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SiemensOPC;
using Opc.Ua;
using Opc.Ua.Client;
using VswService_V7.Config;
using VswService_V7.DataBase.Tables;
using VswService_V7.Utility;
using System.Data;

namespace VswService_V7.OPC.DB
{
    /// <summary>
    /// sampe cut
    /// </summary>
	public  class SampleAddressDbData : SubscriptionDbDataBase
	{
		public SampleAddressDbData(SimaticOPCClient opc, NodeConfig nodes) : base(opc, nodes)
		{
            samplePipe = new SampleInfo(_Location);

        }

        SampleInfo samplePipe;
        string _Location = "SMP";

        public override void CreateSubscription()
		{
			opcServer.QuickAddMonitorItem(opcNodes.SampleAddress.GrpoupName, 
                opcNodes.SampleAddress.PublishTime,
                opcNodes.SampleAddress.SampleTime,
				opcNodes.SampleAddress.GetNodeAddress(), 
                opcNodes.SampleAddress.GetNodeDisplayName(),
				SubscriptionHandle);
			opcServer.QuickAddMonitorItem(opcNodes.SampleResultAddress.GrpoupName, 
                opcNodes.SampleResultAddress.PublishTime,
                opcNodes.SampleResultAddress.SampleTime,
				opcNodes.SampleResultAddress.GetNodeAddress(), 
                opcNodes.SampleResultAddress.GetNodeDisplayName(),
				SubscriptionHandlePipe);
		}

		private void SubscriptionHandle(MonitoredItem monitoredItem, MonitoredItemNotificationEventArgs e)
		{
			MonitoredItemNotification notification = e.NotificationValue as MonitoredItemNotification;
            if (notification == null || notification.Value.Value == null)
                return;
			if (monitoredItem.DisplayName == "SampleAsk")
			{
				bool ask;
				if (bool.TryParse(notification.Value.Value.ToString(), out ask))
				{
					if (ask)
					{
                        Console.WriteLine(_Location + " ask: " + notification.Value);
                        Task.Run(() => HandleSMP());
                    }
					else
					{
						opcServer.WriteValue("false", opcNodes.SampleAddress.SampleRsp);
					}
				}
			}

            if (monitoredItem.DisplayName == "SamplePrintAsk")
            {
                bool ask;
                if (bool.TryParse(notification.Value.Value.ToString(), out ask))
                {
                    if (ask)
                    {
                        Console.WriteLine(_Location + " print ask: ");
                        //打印
                        //PrintSampleResult printSampleResult = new PrintSampleResult();
                        //printSampleResult.Print(true,"",1, sampleResultList);
                        opcServer.WriteValue("true", opcNodes.SampleAddress.SamplePrintRsp);

                    }
                    else
                    {
                        opcServer.WriteValue("false", opcNodes.SampleAddress.SamplePrintRsp);
                    }
                }
            }

        }

        private void SubscriptionHandlePipe(MonitoredItem monitoredItem, MonitoredItemNotificationEventArgs e)
        {
            try
            {
                MonitoredItemNotification notification = e.NotificationValue as MonitoredItemNotification;
                if (notification == null || notification.Value.Value == null)
                    return;
                switch (monitoredItem.DisplayName)
                {
                    case "Batch_No":
                        {
                            samplePipe.batchNo = notification.Value.Value.ToString();
                            break;
                        }
                    case "Pipe_No":
                        {
                            samplePipe.pipeNo = notification.Value.Value.ToString();
                            break;
                        }
                    case "Sample_Type":
                        {
                            samplePipe.sampleType = notification.Value.Value.ToString();
                            break;
                        }
                    case "Sample_Seq":
                        {
                            samplePipe.sampleSeq = notification.Value.Value.ToString();
                            break;
                        }
                    case "Sample_Reason":
                        {
                            samplePipe.sampleReason = notification.Value.Value.ToString();
                            break;
                        }
                    case "Sample_Length":
                        {
                            samplePipe.sampleLength = notification.Value.Value.ToString();
                            break;
                        }
                    //case "Sample_Result":
                    //    {
                    //        samplePipe.sampleResult = notification.Value.Value.ToString();
                    //        break;
                    //    }
                    default:
                        {
                            break;
                        }
                }
            }
            catch(Exception ex)
            {
                throw ex;
            }
        }


        private async Task HandleSMP()
        {
            await Task.Delay(2000);
            if (samplePipe.batchNo.Equals("0") || samplePipe.pipeNo.Equals("0"))
                return;
            //QC CODE 高低位交换
            Console.WriteLine(_Location + " handleqc start record");

            try
            {
                PIPE_INFO.UpdatePipeInfoRecord(samplePipe.GetFullPipeNo(), DateTime.Now, samplePipe.sampleReason, samplePipe.sampleLength, _Location, ref samplePipe);
                SAMPLE_TABLE.NewSampleRecord(samplePipe);
            }
            catch(Exception ex)
            {
                LogHelper.WriteLog("HandleSMP", ex);
            }
            //PRINT!!!!
            opcServer.WriteValue("true", opcNodes.SampleAddress.SampleRsp);
        }        
	}

    
}
